﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using System.Text;
using System.Runtime.Serialization.Json;

namespace msBsc.Cls
{
    /// <summary>
    /// http 帮助
    /// </summary>
    /// Author:蒲崇彬
    /// Creation date:2015/07/28 14:34
    public class HttpWebHelper
    {
        /// <summary>
        /// 获取 淘宝api 获取国家、地区、省份信息（get）
        /// </summary>
        /// <param name="ip">ip</param>
        /// <returns>AreaIpInfo</returns>
        /// Author:蒲崇彬
        /// Creation date:2015/07/28 14:40
        public static AreaIpInfo HttpGetAreaInfo(string ip)
        {
            string url = string.Format("http://ip.taobao.com/service/getIpInfo.php?ip={0}",ip);
            StreamReader reader = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                request.ProtocolVersion = HttpVersion.Version10;
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1";
                //request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                //request.Accept += "Accept-Language: zh-cn,zh;q=0.5\r\n";
                //request.Accept += "Accept-Encoding: gzip, deflate\r\n";
                //request.Accept += "Accept-Charset: GB2312,utf-8;q=0.7,*;q=0.7\r\n";
                //request.Connection = "alive";

                //HttpRequestCachePolicy policy = new HttpRequestCachePolicy(HttpRequestCacheLevel.Default);
                //HttpWebRequest.DefaultCachePolicy = policy;
                //request.CachePolicy = policy;

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                string respHtml = reader.ReadToEnd();

                //json
                DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(AreaIpInfo));

                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(respHtml));

                AreaIpInfo obj = (AreaIpInfo)ser.ReadObject(ms);

                return obj;
            }
            catch (Exception ex)
            {
                return null;
            }
            finally
            {
                reader.Close();
            }
        }
    }

    public class AreaIpInfo
    {

        /// <summary>
        /// 请求状态
        /// </summary>
        public string code { get; set; }

        /// <summary>
        /// 地区信息
        /// </summary>
        public AreaInfo data { get; set; }
    }

    /// <summary>
    /// 地区详情
    /// </summary>
    public class AreaInfo
    {
        /// <summary>
        ///国家
        /// </summary>
        public string country { get; set; }

        public string country_id { get; set; }

        /// <summary>
        ///市（自治区或直辖市）
        /// </summary>
        public string city { get; set; }

        public string city_id { get; set; }

        /// <summary>
        ///区
        /// </summary>
        public string county { get; set; }

        public string county_id { get; set; }

        /// <summary>
        /// 省
        /// </summary>
        public string region { get; set; }

        public string region_id { get; set; }
        /// <summary>
        /// 地区（东北，华南）
        /// </summary>
        public string area { get; set; }

        public string area_id { get; set; }

        /// <summary>
        /// 网络运营商
        /// </summary>
        public string isp { get; set; }

        public string isp_id { get; set; }

        public string ip { get; set; }
    }
}